package com.LensBlaster.upload.Flickr;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

/**
 * Allows the user to authorize LensBlaster with his or her Flickr account,
 * saving the authorization token in the process.
 */
public class WebAuthentication extends Activity {

	
	
    /**
     * Called when the activity is first created.  The Authentication URL is
     * loaded with a WebView.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Set up view
        setContentView(com.LensBlaster.R.layout.flickr_auth);
        setTitle("Authorize LensBlaster");
        
        // Get Auth URL and set up WebView
        Intent i = this.getIntent();
        Bundle b = i.getExtras();
        String url = b.getString(android.app.SearchManager.QUERY);
        url = url.replace("www.flickr.com", "m.flickr.com");
        WebView wv = (WebView)findViewById(com.LensBlaster.R.id.webview);
        wv.setWebViewClient(new WebViewClient(){
        	
        	@Override
        	public boolean shouldOverrideUrlLoading(WebView view, String url){
        		
        		// Don't leave the WebView
        		return false;
			}
        });
        WebSettings ws = wv.getSettings();
        ws.setJavaScriptEnabled(true);
        wv.loadUrl(url);
        
        android.util.Log.d("lens url=",url);
        
        // Set up the "Finish" button
        Button backButton = (Button)findViewById(com.LensBlaster.R.id.finish);
        backButton.setOnClickListener(new View.OnClickListener() {

        	/**
        	 * Return to the previous activity.
        	 */
        	public void onClick(View view) {
        		Intent i = new Intent();
        	    setResult(RESULT_OK,i);
        	    finish();
        	}
          
        }); 
    }
}
